#include "Common.hpp"
void Usage(const char *argv)
{
    cout << argv << " server_ip,server_port" << endl;
}
int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        Usage(argv[0]);
        exit(USAGE_ERRO);

    }
    int listensockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (listensockfd < 0)
    {
        LOG(LogLevel::FATAL) << "socket erro";
        exit(SOCKET_ERRO);
    }
    string ip = argv[1];
    uint16_t port = stoi(argv[2]);
    INETADDR client(ip, port);
    int connectsockfd = connect(listensockfd, client.NetAddrPtr(), client.NetAddrLen());
    if (connectsockfd == -1)
    {
        LOG(LogLevel::FATAL) << "connect erro";
        exit(CONNECT_ERRO);
    }
    while (true)
    {
        cout << "Please Enter# ";
        string input;
        getline(cin, input);
        ssize_t w = write(listensockfd, input.c_str(), input.size());
        if (w == -1)
        {
            LOG(LogLevel::INFORMATION) << "write erro";
        }
        char buf[1024] = {0};
        ssize_t r = read(listensockfd, buf, sizeof(buf) - 1);
        if (r > 0)
        {
            buf[r] = '\0';
            cout << "server say# " << buf << endl;
        }
    }
    close(listensockfd);
    close(connectsockfd);

    return 0;
}